package com.apes.fn.scm.internal.help.service;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailReturn.RetailReturn;
import com.apes.fn.scm.retailReturn.RetailReturnRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Store;
import com.apes.scm.masterdata.frame.repository.StoreRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.repository.VoucherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 通过场景流调用
 *
 * 内协退回入口
 */
@Service("internalHelpAcceptanceService")
public class InternalHelpAcceptanceService extends DomainService {

    @Autowired
    private RetailReturnRepository retailReturnRepository;
    @Autowired
    private  VoucherRepository voucherRepository;
    @Autowired
    private InternalHelpReturnStartService internalHelpReturnStartService;


    /**
     * 场景流调用
     * 开始退货验收检查
     * @param request retailReturnId
     */
    public void startReturnAcceptanceCheck(SimpleRequest request) {
        RetailReturn retailReturn = retailReturnRepository.findOne(request.get("id").toString());
        returnSinopecInovice(retailReturn);     // 增加中石化账单退回
        RetailOrder retailOrder = retailReturn.getSaleOrder();
        if (Objects.isNull(retailOrder.getSource())
                && LogicalUtil.notIn(retailOrder.getOrderType(), "annualAudit", "detectReport")) return;

        if ( LogicalUtil.in(retailOrder.getOrderType(), "server", "annualAudit", "detectReport")
                || "RO003".equals(retailOrder.getScene().getId()) ) {
            internalHelpReturnStartService.process(retailReturn);       // 这里开始检查是否有对应业务退回
        }
    }


    private void returnSinopecInovice(RetailReturn retailReturn){
        // 判断是否为中石化门店
        Store store = SpringManager.getBean(StoreRepository.class).findByDept(retailReturn.getDept());
        if (!store.isSinopec()) return;
        if (StringUtils.isEmpty(retailReturn.getSaleOrder().getSinopecNumber())) return;

        // 查找对应的零售退回凭证
        List<Voucher> returnVouchers = voucherRepository.findAll(JpaDsl.toCriteriaByEq("source", retailReturn.getId(), "origin",
                retailReturn.getId(), "sourceBusinessType", retailReturn.getInnerBusinessType()));
        if (returnVouchers.size() > 1) throw new RuntimeException("中石化生成退回账单，找到多个零售退回凭证，请检查");
        if (returnVouchers.size() == 0) throw new RuntimeException("中石化生成退回账单，未找到零售退回凭证，请检查");

        Map invoice = invoke("voucher.flow.invoice", MapUtil.mapped(returnVouchers.get(0)));

        // 查询中石化客户
        PartyRole sinopec = SpringManager.getBean(PartyRoleRepository.class).findOne("C0002496");
        invoice.put("partner", MapUtil.mapped(sinopec));

        invoke("invoice.create", MapUtil.mapped(invoice));
    }

}
